package Statistics;
import java.util.Date;
import java.util.*;
import java.util.Scanner;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;



public class WebPageStatisticsSystem {
    //用ArrayList储存对象
    static ArrayList<WebPage> list = new ArrayList<>();
    TreeSet<Integer> ts = new TreeSet<>();
    static int x=0;
    public static void main(String[] args) {
        loop:
        while (true) {
            System.out.println("1.访问页面,显示访问时间");
            System.out.println("2.排行榜");
            System.out.println("3.总访问量");
            System.out.println("4.平均访问量");
            System.out.println("5.清空访问记录");
            System.out.println("6.显示页面最后的访问时间");
            System.out.println("7.退出");
            System.out.println("请输入您的选择：");
            Scanner sc = new Scanner(System.in);
            String choose = sc.nextLine();
            switch (choose) {
                case "1" -> {
                    x++;
                    System.out.println("1.访问页面,显示访问时间");
                    addWebPage(list);
                }
                case "2" -> {
                    System.out.println("2.排行榜(从高到低，若相同则按首字母顺序排）");
                    sortWebPage(list);
                }
                case "3" -> {
                    System.out.println("3.总访问量");
                    totalVisting(list);
               }
                case "4" -> {
                    System.out.println("4.平均访问量");
                    everageWebPage(list);
                }
                case "5" -> {
                    System.out.println("5.清空访问记录");
                    clearWebPage(list);
                }
                case "6" -> {
                    System.out.println("6.显示页面的最后访问时间");
                    findLastTime(list);
                }
                case "7" -> {
                    System.out.println("7.退出");
                    break loop;
                }
                default -> {
                    System.out.println("没有该选项");
                }
            }
        }
    }
    public static void addWebPage(ArrayList<WebPage> list){

        WebPage webPage = new WebPage();
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入url:");
        String url = sc.nextLine();
        boolean flag = contains(list,url);
        int index = getIndex(list,url);
        if(flag){
            Date d1 = new Date();
           System.out.println(d1);
           WebPage p = list.get(index);
           p.setLastVisit(d1);
           Integer count=p.getVisits()+1;
           p.setVisits(count);
           System.out.println("该网址已访问了"+count+"次.");
        }
        else{
            webPage.setUrl(url);
            list.add(webPage);
            Integer v=1;
            webPage.setVisits(v);
            Date d1 = new Date();
            webPage.setLastVisit(d1);
            System.out.println("访问时间为："+d1);
            System.out.println("新网址添加成功！");
        }
    }
   public static void totalVisting(ArrayList<WebPage> list){
        System.out.println("总访问量为："+x);
   }
   //按访问次数从高到低排序
   public static void sortWebPage(ArrayList<WebPage> list) {
       if (list.isEmpty()) {
           System.out.println("访问箱里空空的");
       } else {
           TreeSet<WebPage> ts = new TreeSet<>(new Comparator<WebPage>() {
               @Override
               public int compare(WebPage o1, WebPage o2) {
                   int i = o2.getVisits() - o1.getVisits();
                   i = i == 0 ? o2.compareTo(o1) : i;
                   i = i == 0 ? o1.getUrl().compareTo(o2.getUrl()) : i;
                   return i;
               }
           });
           for (int i = 0; i < list.size(); i++) {
               WebPage p = list.get(i);
               ts.add(p);
           }
           System.out.println(ts);
       }
   }
   public static void findLastTime(ArrayList<WebPage> list){
         Scanner sc = new Scanner(System.in);
        System.out.println("请输入你想查询的网址：");
         String url = sc.nextLine();
         int index=getIndex(list,url);
         if(index==-1){
             System.out.println("Error");
         }else {
             WebPage p = list.get(index);
             System.out.println(p.getUrl() + "页面的最后访问时间为：" + p.getLastVisit());
         }
   }
   public static void clearWebPage(ArrayList<WebPage> list){
        x=0;
        for(int i=0;i<list.size();){
            WebPage p = list.get(i);
            list.remove(p);
        }
        System.out.println("已清空所有的访问统计！");
   }
   public static void everageWebPage(ArrayList<WebPage> list){
        double everage=0;
        everage=1.0*x/list.size();
        System.out.println("平均访问量为："+everage);
   }
    public static boolean contains(ArrayList<WebPage> list,String url){
        for(int i=0;i<list.size();i++){
            WebPage p = list.get(i);
            if(p.getUrl().equals(url)){
                return true;
            }
        }
        return false;
    }
    private static int getIndex(ArrayList<WebPage> list,String url){
        for(int i=0;i<list.size();i++){
            WebPage p = list.get(i);
            if(p.getUrl().equals(url)){
                return i;
            }
        }
        return -1;
    }
}

